(function(){
	var WorkflowList = {

		_LangifyStep: function(wf) {
			var $sentence = $('<div class="sentence"></div>');
			
			$.each(wf.steps, function(index, json){
				var step = $.parseJSON(json);
				
				var part = '';				
				if (index > 0) {
					part = '<p>then ';
				} else {
					part = '<p>';
				}

				for (var stepClass in step.step) {
					switch(stepClass) {
						case 'Workflow::ApproveStep':
							part += 'Requires approval by ';
							part += '<span class="approver-configurator configurator">';
							part += step.step[stepClass].approver;
							part += '</span>';
							part += '</p>';
							break;
						case 'Workflow::EmailStep':
							part += 'Send email from ';
							part += '<span class="approver-configurator configurator">';
							part += step.step[stepClass].from;
							part += '</span>';
							part += ' to ';
							part += '<span class="email-configurator configurator">';
							part += step.step[stepClass].to;
							part += '</span>';
							part += '</p>';

							break;
						case 'Workflow::IfElseStep':
							break;
						case 'Workflow::BeginStep':
							break;
						case 'Workflow::EndStep':
							break;
						case 'Workflow::GotoStep':
							break;
					}
				}

				$sentence.append(part);
				
			});
			
			return $sentence;
		},

		_LoadWorkflows: function(ruleClassId) {
			var widget = this;
			var endpoint = '/workflows';
			
			if (ruleClassId) {
				endpoint += '/' + ruleClassId;
			}
			
			var xhr = $.wGet(endpoint, null);
			xhr.success(function(data, textStatus, jqXhr) {
				$.each(data, function(index, workflow){
					// create new element
					var $workflow = $('<div class="workflow-config"></div>');
					var ruleClassName = "";
					
					switch( workflow.rule_class_id ) {
						case 1:
							ruleClassName = "Apollo Test Config";
							break;
						case 2:
							ruleClassName = "Sabre Test Config";
							break;
						case 3:
							ruleClassName = "Worldspan Test Config";
							break;
						case 4:
							ruleClassName = "Amadeus Test Config";
							break;
					}
					
					
					// save workflow data in element
					$workflow.data('workflow', workflow);
					
					// add the name and description
					$workflow.append('<p class="workflow-name">' + workflow.name + ' (' + ruleClassName + ')</p>');
					$workflow.append('<p class="workflow-description">' + workflow.description + '</p>');
					
					// create toolbar
					var $tools = $('<div class="workflow-tools"></div>')
					$tools.append('<button class="workflow-enable-disable">Enable</button>')
					$tools.append('<button class="workflow-edit">Open in Advanced Mode</button>');
					$workflow.append($tools);
					
					$workflow.append(widget._LangifyStep(workflow));
					
					$workflow.bind('show', function(){
						$workflow.find('.workflow-tools').show();
						$workflow.find('.workflow-description').show();
						$workflow.find('.sentence').show();
						$workflow.addClass('selected');
					});					

					$workflow.bind('hide', function(){
						$workflow.find('.workflow-tools').hide();
						$workflow.find('.workflow-description').hide();
						$workflow.find('.sentence').hide();
						$workflow.removeClass('selected');
					});
					$workflow.trigger('hide');
					
					$workflow.bind('enabled', function(){
						$workflow.addClass('workflow-enabled');
						$tools.find('.workflow-enable-disable').hide();
					});
					
					$workflow.bind('disabled', function(){
						$workflow.removeClass('workflow-enabled');
						$tools.find('.workflow-enable-disable').show();
					});
					
					if (workflow.enabled === true) {
						$workflow.trigger('enabled');
					}
					
					// bind some events
					$workflow.bind('click', function() {
						// hide description and tools for all configs
						widget.list.find('.workflow-config').trigger('hide');
						
						// show tools and description for selected workflow
						$workflow.trigger('show');

					});
					
					$tools.find('.workflow-enable-disable').bind('click', function(){
						$.wPost('/workflow/' + workflow.id + '/enable', null).success(function(data, textStatus, jqXhr){
							// update data
							$workflow.data('workflow', data)
							
							// disable all workflows with the same rule class
							// as the just enabled workflow
							widget.list.find('.workflow-config').each(function(index, curWorkflowEle){
								var curData = $(curWorkflowEle).data('workflow');
								if (curData.rule_class_id === data.rule_class_id) {
									$(this).trigger('disabled');
								}
							});

							// enable the currently enabled workflow
							$workflow.trigger('enabled');
						});
						return false;
					});
					
					$tools.find('.workflow-edit').bind('click', function(){
						alert("Edit workflow in advanced mode");
						return false;
					})					
					
					widget.list.append($workflow);
				});
			});
		},
		
		// required function. Automatically called when widget is created
		_create: function() {
			// do nothing
		},

		// Once the element is created, all other calls to the widget
		// name where the first parameter is not a string will call the _init()
		// method; if options are passed, the .option() method will be called
		// before the _init() method.
		_init: function() {
			var widget = this;
			if (widget.list) {
				widget.list.remove();
			}
			widget.list = $('<div id="workflow-config-list"></div>');
			widget.element.append(widget.list);
			widget._LoadWorkflows();
		},

		options: {
		},


		destroy: function() {
			this.list.remove();
		},
		
		LoadWorkflows: function(ruleClassId) {
			// remove all current workflows
			this.list.find('.workflow-config').remove();
			// load new workflows
			this._LoadWorkflows(ruleClassId);
		}
	};
	// register widget
	$.widget("ui.WorkflowList", WorkflowList);
})();
